package com.android.systemui.ios.wall;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Outline;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.android.systemui.ios.R;

public class IpPreviewItemLayout extends FrameLayout {

    private final int mCorner;
    private final int mWidth;

    private ImageView mImageView;
    private ImageView mImageAddIcon;

    public IpPreviewItemLayout(Context context) {
        this(context, null, 0);
    }

    public IpPreviewItemLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IpPreviewItemLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        final Resources res = context.getResources();
        mCorner = res.getDimensionPixelSize(R.dimen.pager_bg_corner_radius_size);
        mWidth = res.getDimensionPixelSize(R.dimen.pager_layout_width);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mImageView = (ImageView) findViewById(android.R.id.icon);
        mImageAddIcon = (ImageView) findViewById(android.R.id.icon1);

        setClipToOutline(true);
        setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), mCorner);
            }
        });
    }

    public void setImageBitmap(Bitmap bitmap, boolean showAddIcon) {
        if (bitmap != null) {
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();

            int targetH = mWidth * h / w;
            if (showAddIcon) {
                mImageAddIcon.setVisibility(VISIBLE);
                mImageView.setImageBitmap(Bitmap.createBitmap(mWidth, targetH, Bitmap.Config.ARGB_8888));
                mImageView.setBackgroundResource(R.drawable.ip_ic_keyguard_preview_add_pager_bg);
            } else {
                mImageAddIcon.setVisibility(GONE);
                mImageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, mWidth, targetH, true));
            }
        }
    }
}
